package com.khalil.kkadmin.ai.rag.knowledgeset.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.khalil.kkadmin.ai.rag.knowledgeset.entity.RagKnowledgeSegment;
import com.khalil.kkadmin.ai.rag.knowledgeset.repository.RagKnowledgeSegmentRepository;
import com.khalil.kkadmin.ai.rag.knowledgeset.service.RagKnowledgeSegmentService;
import com.khalil.kkadmin.infrastructure.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RagKnowledgeSegmentServiceImpl extends BaseService<RagKnowledgeSegmentRepository, RagKnowledgeSegment, Long> implements RagKnowledgeSegmentService {
    public RagKnowledgeSegmentServiceImpl(RagKnowledgeSegmentRepository repository) {
        super(repository);
    }

    @Override
    protected RagKnowledgeSegment str2CreateEntity(String json) {
        return JSON.parseObject(json, RagKnowledgeSegment.class);
    }

    @Override
    protected RagKnowledgeSegment str2UpdateEntity(String json) {
        return JSON.parseObject(json, RagKnowledgeSegment.class);
    }

    @Override
    public List<RagKnowledgeSegment> list(String params) {
        JSONObject jsonObject = JSON.parseObject(params);
        Long knowledgeSetId = jsonObject.getLong("knowledgeSetId");
        if (knowledgeSetId == null) {
            return List.of();
        }
        return repository.findAllByKnowledgeSetId(knowledgeSetId);
    }

    @Transactional
    @Override
    public void deleteByKnowledgeTextId(Long knowledgeTextId) {
        repository.deleteByKnowledgeTextId(knowledgeTextId);
    }
}
